//
//  SYS_LoginModel.m
//  System_App
//
//  Created by 魏配军 on 2017/11/28.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_LoginModel.h"


@implementation SYS_Admin
- (NSDictionary *)dictionaryValue{
    NSArray *array = [self allPropertyNames];
    NSMutableDictionary *mdc = [NSMutableDictionary new];
    for (int i = 0; i < array.count; i ++) {
        //        NSString *sKey  = array[i];
        NSMutableString *sKey = [[NSMutableString alloc] initWithString:array[i]];
        //        NSString * s =  [array[i] substringWithRange:NSMakeRange(1, 1)];
        //        [sKey replaceCharactersInRange:NSMakeRange(0, 2) withString:[s lowercaseString]];
        //        if ([sKey hasPrefix:@"img"]) {
        //            continue;
        //        }
        NSString *sValue = [self valueForKey:array[i]];
        sValue = sValue? sValue: @"";
        [mdc setValue:sValue forKey:sKey];
    }
    return mdc;
}

- (NSArray *)allPropertyNames{
    NSMutableArray *allNames    = [[NSMutableArray alloc] init];
    unsigned int propertyCount  = 0;
    NSString *sClass = NSStringFromClass([self class]);
    objc_property_t *propertys  = class_copyPropertyList(objc_getClass([sClass UTF8String]), &propertyCount);
    for (int i = 0; i < propertyCount; i ++) {
        objc_property_t property   = propertys[i];
        const char *propertyName   = property_getName(property);
        [allNames addObject:[NSString stringWithUTF8String:propertyName]];
    }
    free(propertys);
    return allNames;
}
@end

@implementation SYS_User
- (NSDictionary *)dictionaryValue{
    NSArray *array = [self allPropertyNames];
    NSMutableDictionary *mdc = [NSMutableDictionary new];
    for (int i = 0; i < array.count; i ++) {
        //        NSString *sKey  = array[i];
        NSMutableString *sKey = [[NSMutableString alloc] initWithString:array[i]];
        //        NSString * s =  [array[i] substringWithRange:NSMakeRange(1, 1)];
        //        [sKey replaceCharactersInRange:NSMakeRange(0, 2) withString:[s lowercaseString]];
        //        if ([sKey hasPrefix:@"img"]) {
        //            continue;
        //        }
        NSString *sValue = [self valueForKey:array[i]];
        sValue = sValue? sValue: @"";
        [mdc setValue:sValue forKey:sKey];
    }
    return mdc;
}

- (NSArray *)allPropertyNames{
    NSMutableArray *allNames    = [[NSMutableArray alloc] init];
    unsigned int propertyCount  = 0;
    NSString *sClass = NSStringFromClass([self class]);
    objc_property_t *propertys  = class_copyPropertyList(objc_getClass([sClass UTF8String]), &propertyCount);
    for (int i = 0; i < propertyCount; i ++) {
        objc_property_t property   = propertys[i];
        const char *propertyName   = property_getName(property);
        [allNames addObject:[NSString stringWithUTF8String:propertyName]];
    }
    free(propertys);
    return allNames;
}
@end

@implementation SYS_SessionInfo
- (NSDictionary *)dictionaryValue{
    NSArray *array = [self allPropertyNames];
    NSMutableDictionary *mdc = [NSMutableDictionary new];
    for (int i = 0; i < array.count; i ++) {
        //        NSString *sKey  = array[i];
        NSMutableString *sKey = [[NSMutableString alloc] initWithString:array[i]];
        //        NSString * s =  [array[i] substringWithRange:NSMakeRange(1, 1)];
        //        [sKey replaceCharactersInRange:NSMakeRange(0, 2) withString:[s lowercaseString]];
        //        if ([sKey hasPrefix:@"img"]) {
        //            continue;
        //        }
        NSString *sValue = [self valueForKey:array[i]];
        sValue = sValue? sValue: @"";
        [mdc setValue:sValue forKey:sKey];
    }
    return mdc;
}

- (NSArray *)allPropertyNames{
    NSMutableArray *allNames    = [[NSMutableArray alloc] init];
    unsigned int propertyCount  = 0;
    NSString *sClass = NSStringFromClass([self class]);
    objc_property_t *propertys  = class_copyPropertyList(objc_getClass([sClass UTF8String]), &propertyCount);
    for (int i = 0; i < propertyCount; i ++) {
        objc_property_t property   = propertys[i];
        const char *propertyName   = property_getName(property);
        [allNames addObject:[NSString stringWithUTF8String:propertyName]];
    }
    free(propertys);
    return allNames;
}
@end

static SYS_LoginModel *pData = NULL;
@implementation SYS_LoginModel

+ (SYS_LoginModel *)instance{
    @synchronized (self) {
        if (pData == NULL) {
            pData = [[SYS_LoginModel alloc] init];
            if (!pData.sessionInfo) {
                pData.sessionInfo = [SYS_SessionInfo new];
            }
            if (!pData.user){
                pData.user = [SYS_User new];
            }
            if (!pData.admin) {
                pData.admin = [SYS_Admin new];
            }

        }
    }
    return pData;
}

- (void)reset{
    pData = NULL;
}

#pragma mark - 属性转字典
- (NSDictionary *)dictionaryValueBy:(SYS_LoginModel *)info{
    NSArray *array = [self allPropertyNames];
    NSMutableDictionary *mdc = [NSMutableDictionary new];
    for (int i = 0; i < array.count; i ++) {
        //例:array[i] == sStoreInfo ，以下方法是吧字符串转化为storeInfo
        NSMutableString *sKey = [[NSMutableString alloc] initWithString:array[i]];
        //        NSString * s =  [array[i] substringWithRange:NSMakeRange(1, 1)];
        //        [sKey replaceCharactersInRange:NSMakeRange(0, 2) withString:[s lowercaseString]];
        
        //        if ([sKey hasPrefix:@"img"]) {
        //            continue;
        //        }
        
        id sValue = [self valueForKey:array[i]];
        
        if ([sKey isEqualToString:@"user"]) {
            SYS_User *userInfo = info.user;
            sValue = [userInfo dictionaryValue];
            
        }
        if ([sKey isEqualToString:@"sessionInfo"]) {
            SYS_SessionInfo *sessionInfo = info.sessionInfo;
            sValue = [sessionInfo dictionaryValue];
        }
        if ([sKey isEqualToString:@"admin"]) {
            SYS_Admin *ownerInfo =  info.admin;
            sValue = [ownerInfo dictionaryValue];
        }
        
        sValue = sValue? sValue: @"";
        [mdc setValue:sValue forKey:sKey];
        
        
        //        sValue = sValue? sValue: @"";
        //        [mdc setValue:sValue forKey:sKey];
    }
    return mdc;
}


- (NSArray *)allPropertyNames{
    NSMutableArray *allNames    = [[NSMutableArray alloc] init];
    unsigned int propertyCount  = 0;
    NSString *sClass = NSStringFromClass([self class]);
    objc_property_t *propertys  = class_copyPropertyList(objc_getClass([sClass UTF8String]), &propertyCount);
    for (int i = 0; i < propertyCount; i ++) {
        objc_property_t property   = propertys[i];
        const char *propertyName   = property_getName(property);
        [allNames addObject:[NSString stringWithUTF8String:propertyName]];
    }
    free(propertys);
    return allNames;
}




@end
